package com.woupla.rdv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.jboss.solder.logging.Logger;
import org.primefaces.model.SortOrder;

import com.woupla.model.Company;
import com.woupla.model.Companyproduit;
import com.woupla.model.ExceptionPlaning;
import com.woupla.model.Planing;
import com.woupla.model.PlaningPK;
import com.woupla.model.Rdv;
import com.woupla.model.User;

@Stateless
public class RdvEJB {
	@Inject
	private Logger log;

	@Inject
	private EntityManager em;

	public List<Planing> findPlaningUser(User user, int numSemDeb, int numSemFin) {
		TypedQuery<Planing> query = em.createNamedQuery("findByEmployeeSemaine", Planing.class);
		query.setParameter("employee", user.getId());
		query.setParameter("debut", numSemDeb);
		query.setParameter("fin", numSemFin);
		List<Planing> planing = query.getResultList();
		if (planing.isEmpty())
		{
			TypedQuery<Planing> queryStatic = em.createNamedQuery("findByEmployeeStatic", Planing.class);
			queryStatic.setParameter("employee", user.getId());
			planing = queryStatic.getResultList();
		}
		return planing;

	}

	public List<Rdv> findAllRdvUser(User user) {
		TypedQuery<Rdv> query = em.createNamedQuery("Rdv.findAllForUser", Rdv.class);
		query.setParameter("userId", user);
		List<Rdv> rdvs = query.getResultList();
		return rdvs;

	}

	public int countRdvUser(User user) {
		Query query = em.createNamedQuery("Rdv.countForUser");
		query.setParameter("userId", user);
		int count = ((Long) query.getSingleResult()).intValue();
		return count;

	}

	public List<Rdv> findAllRdvUser(User user, int first, int pageSize) {
		TypedQuery<Rdv> query = em.createNamedQuery("Rdv.findAllForUser", Rdv.class);
		query.setParameter("userId", user);
		query.setFirstResult(first);
		query.setMaxResults(pageSize);
		List<Rdv> rdvs = query.getResultList();
		return rdvs;

	}

	public void addRdv(User employee, User customer, Date start, Date end, Companyproduit produit) {
		Rdv rdv = new Rdv();
		rdv.setEmployee(employee);
		rdv.setCompanyproduit(produit);
		rdv.setUser(customer);
		rdv.setDateDebut(start);
		rdv.setDateFin(end);
		rdv.setConfirme(1);
		em.persist(rdv);
	}

	public Planing findStaticUserPlaning(User user) {
		TypedQuery<Planing> query = em.createNamedQuery("Planing.findByEmployeeStatic", Planing.class);
		query.setParameter("employee", user);
		Planing planing = null;
		try {
			planing = query.getSingleResult();
		} catch (NoResultException e) {
			planing = new Planing();
			planing.setPlaningPK(new PlaningPK(0, 0));
			planing.setUser(user);
			planing.setCompany(user.getCompany());
			planing.setStatus(1);
		} catch (Exception e) {
			planing = new Planing();
			planing.setPlaningPK(new PlaningPK(0, 0));
			planing.setUser(user);
			planing.setCompany(user.getCompany());
			planing.setStatus(1);
			e.printStackTrace();
		}
		return planing;
	}

	public void saveStaticUserPlaning(Planing planing) {
		// em.merge(planing.getCompany());
		if (planing.getPlaningPK().getId() < 1) {
			em.persist(planing);
		} else {
			em.merge(planing);
		}

	}
	
	public List<ExceptionPlaning> findExceptionPlaning(Company company, int first, int pageSize, String sortField, SortOrder sortOrder,
			Map<String, String> filters){
		String queryString = "SELECT e FROM ExceptionPlaning e WHERE e.company = " + company.getId();
		String filterString = "";
		for (Map.Entry<String, String> filter : filters.entrySet()) {
			filterString = " AND " + filter.getKey() + " LIKE '%" + filter.getValue() +"%'";
		}
		if (filterString != null)
			queryString = queryString + filterString;
		if (sortField != null){
			if(sortOrder == SortOrder.ASCENDING)
				queryString =  queryString + " ORDER BY " + sortField + " ASC";
			else
				queryString =  queryString + " ORDER BY " + sortField + " DESC";
		}
			
		
		TypedQuery<ExceptionPlaning> query = em.createQuery(queryString, ExceptionPlaning.class);
		query.setFirstResult(first);
		query.setMaxResults(pageSize);
		return query.getResultList();
	}

}
